<?php
/**
 * 任务分配处理
 * @author ice <iceinto@mallog.com.cn>
 *
 */
class TaskAllotModel extends BaseModel
{
	/**
	 * 分配一个任务给对应人员
	 * @param int $uid
	 * @return array
	 * @author ice <iceinto@mallog.com.cn>
	 */
	public function allotUserOne($uid){
		$task = null;
		//获取任务没有被分配并且还是可以分配的状态
		$sql = 'SELECT id,title,uid,mid,fid,bid FROM m_task WHERE id NOT IN(SELECT tid FROM m_task_allot) AND status = 1 ORDER BY create_time ASC LIMIT 1';
		$rs = $this->query($sql);
		if($rs){
			$task = $rs[0];
			//进行任务安排
			$data = array();
			$data['tid'] = $task['id'];
			$data['uid'] = $uid;
			$data['create_time'] = time();
			$data['status'] = 2;
			//执行任务分配
			$rs = $this->doAllot($data);
			if(!$rs){
				//如果分配没有成功返回值赋值为空
				$task = null;
			}else{
				D('Task')->updateStatus($task['id'], 2);
				D('Queue')->addOne($task, $uid);
			}
		}
		return $task;
	}

	/**
	 * 执行一个任务分配
	 * TODO 当前对于任务进行单一安排，添加对任务分配重复性的判断
	 * @return int
	 * @author ice <iceinto@mallog.com.cn>
	 */
	public function doAllot($data){
		$map = array();
		$map['tid'] = $data['tid'];
		$count = $this->where($map)->count();
		$rs = 0;
		if($count){

		}else{
			$rs = $this->add($data);
		}
		return $rs;
	}

	/**
	 * 获取一个任务分配情况
	 * @param int $tid
	 * @return array
	 * @author ice <iceinto@mallog.com.cn>
	 */
	public function getOneByTid($tid, $field = 'id,create_time,status'){
		$map = array();
		$map['tid'] = $tid;
		return $this->getByWhere($map, $field);
	}

	/**
	 * Set task start time
	 * @param int $tid task id
	 * @return boolean
	 * @author ice <iceinto@mallog.com.cn>
	 */
	public function startTime($tid){
		$map = array();
		$data = array();
		$data['start_time'] = time();
		$map['tid'] = $tid;
		return $this->where($map)->save($data);
	}

	/**
	 * Get task executives id number
	 * @param int $tid task id
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getUserNum($tid){
		$num = null;
		$allot = $this->getOneByTid($tid, 'uid');
		if($allot){
			$num = D('User')->getNum($allot['uid']);
		}
		return $num;
	}

	/**
	 * Get today's assigned amount
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function dayCount(){
		$map = array();
		$dayTime = $this->getThisDayTime();
		$map['create_time'] = array('GT', $dayTime);
		return $this->customCount($map);
	}

	/**
	 * Get today's assigned amount
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function usersDayCount(){
		$map = array();
		$dayTime = $this->getThisDayTime();
		$map['create_time'] = array('GT', $dayTime);
		return $this->customUserCount($map);
	}

	/**
	 *
	 * @param array $map
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function customCount($map){
		return $this->where($map)->count();
	}
	/**
	 *
	 * @param array $map
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function customUserCount($map){
		$fields = 'uid, count(1) as count';
		$users = $this->field($fields)->where($map)->group('uid')->select();
		foreach($users as $key=>$user){
			$users[$key]['num'] = D('User')->getNum($user['uid']);
		}
		return $users;
	}

	/**
	 * 获取当前用户领取任务详细
	 * @param int $uid
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	Public function getUserDayAllot($uid){
		$uid = intval($uid);
		if($uid){
			$map = array();
			$dayTime = $this->getThisDayTime();
			$map['create_time'] = array('GT', $dayTime);
			$map['uid'] = $uid;
			$fields = 'tid, image';
			$tids = $this->where($map)->field($fields)->select();
			//获取活动信息
			if(count($tids) > 0){
				$TaskModel = D('Task');
				$MallModel = D('Mall');
				foreach ($tids as $key=>$tid){
					if($tid['image']){
						$tids[$key]['image_url'] = $this->getManagedUrl($tid['image'], 'task');
					}
					//获取任务信息
					unset($map);
					$map['id'] = $tid['tid'];
					$task = $TaskModel->getList($map, null, 'id,title,status,mid,fid,bid');
					if($task){
						$tids[$key] = array_merge($tids[$key], $task['0']);
						$tids[$key] = $MallModel->setAddress($tids[$key]);
					}
				}
				return $tids;
			}
		}
		return false;
	}

	/*任务统计处理方法*/

	/**
	 * 根据条件获取分配数量
	 * @param array $map
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getAllotCount($map){
		return $this->field('uid,COUNT(1) AS all_count')->where($map)->group('uid')->select();
	}

	/**
	 * 获取选择年月的统计数据
	 * @param int $y
	 * @param int $m
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getAllStatisics($y = 0, $m = 0){
		import("@.ORG.Util.Date");
		$Date = new Date();
		if($y && $m){
			$Date = new Date($y.'-'.$m.'-01');
		}
		$fireTime = $Date->firstDayOfMonth()->format("%Y-%m-%d");
		$lastTime = $Date->lastDayOfMonth()->format("%Y-%m-%d");

		$fireTime = strtotime($fireTime);
		$lastTime = strtotime($lastTime);

		$map = array();
		$map['create_time'] = array(array('egt', $fireTime), array('elt', $lastTime));
		return $this->getAllotCount($map);
	}
}